﻿using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;
using Nemerle.Compiler;

using System;
using System.Collections.Generic;
using System.Linq;

using N2 = Nemerle2.Compiler;
using PT = Nemerle.Compiler.Parsetree;
using PEG = Nemerle.Peg;

namespace Nemerle2.Parser
{
  public partial class AstConverter
  {
    NLocation(loc : PEG.Location) : Location
    {
      locationConverter.Convert(loc)
    }

    NLocation(located : PEG.Located) : Location
    {
      locationConverter.Convert(located.Location)
    }

    Echo(located : PEG.Located, msg : N2.Msg) : void
    {
      Echo(NLocation(located), msg)
    }

    Echo(loc : PEG.Location, msg : N2.Msg) : void
    {
      Echo(NLocation(loc), msg)
    }

    Echo(loc : Location, msg : N2.Msg) : void
    {
      match(msg.Kind)
      {
        | Error   => Message.Error(loc, msg.Text)
        | Warning => Message.Warning(loc, msg.Text)
        | Hint    => Message.Hint(loc, msg.Text)
      }
    }

    ConvertSplicable(env : GlobalEnv, s : N2.Splicable) : PT.Splicable
    {
      def loc = NLocation(s);

      match(s)
      {
        | Name(id, _, _) => 
          PT.Splicable.Name(PT.Name.NameInCurrentColor(id, loc, env))

        | Expr
        | HalfName => assert(false, "not implemented")

        | Error(msg, _) =>
          Echo(loc, msg);
          PT.Splicable.Name(PT.Name.NameInCurrentColor(" ", loc, env))
      }
    }

    ToStringList(path : N2.LocatedList[N2.PId]) : list[string]
    {
      NList.ToList(path).Map(_.Text)
    }
  }
}
